# coding=utf-8
import json
import sys
import time
from pactverify.matchers import Matcher, Like, EachLike, Term, Enum, PactVerify
import allure
import jsonpath
import pytest

from common.utils import Utils
from common.assert_operator import *
from common.db import *
from common.read_yaml import *
from common.verification import Verification

string_assert = string_assert() # 字符串校验类
pact = Pact() # 契约校验类
execute_sql = sql() # sql集合
utils = Utils()
verification = Verification()
# data = ReadYaml_By_Root_Dict("case/03_roll_back_test/03_1_RollBack.yml").get_yaml_data()  # 读取数据
data = ReadYaml().get_yaml_data(f'{os.path.abspath(__file__).split("_test.py")[0]}.yml')   # 读取数据

@allure.feature('驳回流程')  # 测试报告显示测试功能
class Test_03_1_RollBack():

    # 获取yml文件中[驳回流程1]的数据参数
    approve_process_params = data['驳回流程1']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(approve_process_params,"$[*][0]")
    @pytest.mark.parametrize("title, param, expect", approve_process_params,ids=ids_list)
    def test_RollBack(self, user_login, title, param, expect):
        Logger.info("*************** {}开始执行用例 ***************".format(title))
        allure.dynamic.description(f"{param['description']}")  # 动态解析用例描述在测试报告中展示
        user,process_name = param['user'],param['process_name']
        DBNodeVerify, MessageVerify = '数据库校验', '消息校验'
        contract_format = pact.roll_back_pact_format()
        main = user_login.goto_Approve_Process(user)    # 登录到审批类实例
        process_instance_resp = main.open_process_instance(process_name)   # 通过流程名称打开流程实例
        back_response = main.approve_submit(param,process_instance_resp)    # 驳回审批
        # 审批后校验
        verification.after_approve(process_name, back_response, expect, contract_format, DBNodeVerify,
                                   MessageVerify)
        Logger.info("*************** {}结束执行用例 ***************".format(title))

    # 获取yml文件中[驳回流程2]的数据参数
    approve_process_params = data['驳回流程2']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(approve_process_params,"$[*][0]")
    @pytest.mark.parametrize("title, param, expect", approve_process_params,ids=ids_list)
    def test_RollBack2(self, user_login, title, param, expect):
        Logger.info("*************** {}开始执行用例 ***************".format(title))
        allure.dynamic.description(f"{param['description']}")  # 动态解析用例描述在测试报告中展示
        user,process_name = param['user'],param['process_name']
        DBNodeVerify, MessageVerify = '数据库校验', '消息校验'
        contract_format = pact.roll_back_pact_format()
        main = user_login.goto_Approve_Process(user)    # 登录到审批类实例
        process_instance_resp = main.open_process_instance(process_name)   # 通过流程名称打开流程实例
        back_response = main.approve_submit(param,process_instance_resp)    # 驳回审批
        # 审批后校验
        verification.after_approve(process_name, back_response, expect, contract_format, DBNodeVerify,
                                   MessageVerify)
        Logger.info("*************** {}结束执行用例 ***************".format(title))

    # 获取yml文件中[驳回流程2]的数据参数
    approve_process_params = data['验证可驳回步骤并驳回']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(approve_process_params,"$[*][0]")
    @pytest.mark.parametrize("title, param, expect", approve_process_params,ids=ids_list)
    def test_RollBack2(self, user_login, title, param, expect):
        Logger.info("*************** {}开始执行用例 ***************".format(title))
        allure.dynamic.description(f"{param['description']}")  # 动态解析用例描述在测试报告中展示
        user,process_name = param['user'],param['process_name']
        DBNodeVerify, MessageVerify, CanRejectSteps = '数据库校验', '消息校验', '可驳回步骤校验'
        contract_format = pact.roll_back_pact_format()
        main = user_login.goto_Approve_Process(user)    # 登录到审批类实例
        resp = main.get_process_node(process_name)  #通过当前登陆用户，查询待办流程信息
        node_id = jsonpath.jsonpath(resp.json(), f'$.data.list[?(@.proc_name=="{process_name}")].node_id')[0]   # 获取当前节点ID
        process_instance_resp = main.open_process_instance(process_name)  # 通过流程名称打开流程实例
        can_reject_steps_resp = main.can_reject_steps(node_id)  # 可驳回步骤查询请求
        Logger.debug(f"{CanRejectSteps}：\n{json.dumps(can_reject_steps_resp.json(),ensure_ascii=False)}")
        back_response = main.approve_submit(param,process_instance_resp)    # 驳回审批
        dict_assert(expect[CanRejectSteps],can_reject_steps_resp.json())   # 校验可驳回步骤
        # 审批后校验
        verification.after_approve(process_name, back_response, expect, contract_format, DBNodeVerify,
                                   MessageVerify)

        Logger.info("*************** {}结束执行用例 ***************".format(title))
